package com.zhitong.money.controller;

import com.github.pagehelper.PageInfo;
import com.zhitong.money.entity.AjaxResponse;
import com.zhitong.money.entity.Leave;
import com.zhitong.money.services.api.ILeaveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@Slf4j
@RestController
public class LeaveContro {

    @Autowired
    private ILeaveService service;

    @GetMapping("/leave/{id}")
    public AjaxResponse getLeaveById(@PathVariable String id) throws Exception{
        Leave leaveById = service.findLeaveById(id);
        return AjaxResponse.success(leaveById);
    }

    @GetMapping("/leaves")
    public AjaxResponse getAllLeave() throws Exception{
        List<Leave> allLeave = service.findAllLeave();
        return AjaxResponse.success(allLeave);
    }

    @PostMapping("/leaves")
    public AjaxResponse getLeaveByPage(@RequestBody Map<String,Object> map) throws Exception{
        PageInfo leaveByPage = service.findLeaveByPage(map);
        return AjaxResponse.success(leaveByPage);
    }

    @PostMapping("/leaveMap")
    public AjaxResponse getLeaveByMap(@RequestBody Map<String,Object> map) throws Exception{
        List<Leave> leaveByMap = service.findLeaveByMap(map);
        return AjaxResponse.success(leaveByMap);
    }

    @RequiresPermissions({"addLeave"})
    @PostMapping("/leave")
    public AjaxResponse addLeave(@RequestBody Leave leave) throws Exception{
        service.addLeave(leave);
        return AjaxResponse.success("请假信息已发送!");
    }

    @RequiresPermissions({"updateLeave"})
    @PutMapping("/leave")
    public AjaxResponse updateLeave(@RequestBody Leave leave) throws Exception{
        service.updateLeave(leave);
        return AjaxResponse.success("修改成功!");
    }

    @RequiresPermissions({"delLeave"})
    @DeleteMapping("/leave/{id}")
    public AjaxResponse delLeave(@PathVariable("id")String id) throws Exception{
        service.delLeave(id);
        return AjaxResponse.success("删除假条成功!");
    }


    @DeleteMapping("/selfLeave/{id}")
    public AjaxResponse delSelfLeave(@PathVariable String id) throws Exception{
        service.updateSelfEnable(id,"00");
        return AjaxResponse.success("删除成功!");
    }

    @DeleteMapping("/judgeLeave/{id}")
    public AjaxResponse delJudgeLeave(@PathVariable String id) throws Exception{
        service.updateEnable(id,"00");
        return AjaxResponse.success("删除成功!");
    }

    @RequiresPermissions({"delLeave"})
    @PostMapping("/delleaves")
    public AjaxResponse delLeavesByIds(@RequestBody List<String> ids) throws Exception{
        service.delLeavesByIds(ids);
        return AjaxResponse.success("删除假条成功!");
    }

}


